myApp.controller('MenuSetController', function ($scope, $http, $uibModal) {
    $scope.treedata = [];
    $scope.expandedNodes = [];
    function createSubTree() {
        $.ajax({
            type: "GET",
            url: python_url + '/api/v1/menu/?token_id='+getTokenId(),
            async: false,
            success: function (data) {
                $scope.treedata = [
                    {
                        "menu_id": 0,
                        "menu_level": 0,
                        "child_menu_list": data.data,
                        "parent_menu_id": null,
                        "menu_url": "",
                        "menu_name": "root",
                        "menu_status": 0
                    }
                ];
                $scope.expandedNodes.push($scope.treedata[0]);
                angular.forEach($scope.treedata[0].child_menu_list,function (node) {
                    $scope.expandedNodes.push(node);
                });
            }
        });
    };

    createSubTree();
    $scope.treeOptions = {
        multiSelection: false,
        allowDeselect: false,
        nodeChildren: 'child_menu_list'
    };

    $scope.selectedNode = $scope.treedata[0];

    $scope.del = function (id) {
        if (confirm('确定要删除该菜单么?')) {
            $http.get(python_url + '/api/v1/menu/del?menu_id=' + id+'&token_id='+getTokenId()).success(function (data) {
                if (data.success = true) {
                    createSubTree();
                } else {
                    alert(data.msg);
                }
            })
        }

    };


    $scope.edit = function (node,type) {
        var info = {};
        info.node = node;
        info.type = type;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'edit.html',
            controller: 'editCtrl',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function (result) {
                if (result == 'cancel') {
                    createSubTree();
                }
            },
            function (reason) {
                if (reason == 'cancel') {
                    createSubTree(info);
                }
            });
    };

});


myApp.controller('editCtrl', function ($scope, $uibModalInstance, info, $http) {
    var type = info.type;
    $scope.statu_show = false;
    $scope.menu_status = 0;
    $scope.menu_name = "";
    $scope.menu_url = "";
    $scope.menu_css = "";

    $scope.cancel = function () {
        // 关闭弹出框
        $uibModalInstance.dismiss('cancel');
    };

    if (type == "edit") {
        $scope.statu_show = true;
        $scope.menu_name = info.node.menu_name;
        $scope.menu_url = info.node.menu_url;
        $scope.menu_status = info.node.menu_status;
        $scope.menu_css = info.node.menu_css;
        $scope.menu_id = info.node.menu_id;
    };

    $scope.save = function (id) {
        if (type == 'add') {
            var url = python_url + '/api/v1/menu/add?token_id='+getTokenId()+'&menu_name=' + $scope.menu_name + '&menu_level=' + eval(info.node.menu_level+1) + '&menu_url=' + $scope.menu_url +'&menu_css='+$scope.menu_css+ (info.node.menu_id == 0?'':'&parent_menu_id='+info.node.menu_id);
            $http.get(url).success(function (data) {
                if (data.success == true) {
                    // $uibModalInstance.dismiss('cancel');
                    window.location.reload();
                } else {
                    alert(data.msg);
                }
            });
        } else {
            $http.get(python_url + '/api/v1/menu/update?token_id='+getTokenId()+'&menu_id=' + id + '&menu_name=' + $scope.menu_name + '&menu_url=' + $scope.menu_url +'&menu_css='+$scope.menu_css+ '&menu_status=' + $scope.menu_status).success(function (data) {
                if (data.success == true) {
                   window.location.reload();
                } else {
                    alert(data.msg);
                }
            })


        }

    };
});